
package org.cuatrovientos.erp.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.validation.Valid;

import org.apache.log4j.Logger;
import org.cuatrovientos.erp.business.UserBOInterface;
import org.cuatrovientos.erp.model.User;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * UserController
 * @author CaselHunter
 */
@Controller
public class UserController {

	//Get log4j logger
	private static final Logger logger = 
			Logger.getLogger(UserController.class);

	@Autowired
	private UserBOInterface userBO;

	/**
	 * default constructor
	 */
	public UserController () {
		logger.info("User BEAN instantiated");
	}

	/**
	 * handles default /user or /user/list request
	 * @return
	 */
	@RequestMapping({"/user","/user/list"})
	public ModelAndView userDefault() {
		logger.info("default");

		Map<String, Object> model = new HashMap<String, Object>();

		List<User> userList = userBO.selectAll();
		model.put("cusmtomers", userList );
		
		return new ModelAndView("UserList", model);     
	}


	/**
	 * handles default /user/search request
	 * @param name
	 * @return
	 */
	@RequestMapping(method=RequestMethod.POST,value={"/user/search"})
	public ModelAndView search(
			@RequestParam(value="name", defaultValue="", required=true)
			String name) {
		
		logger.info("search");

		Map<String, Object> model = new HashMap<String, Object>();

		List<User> userList = userBO.search(name);
		model.put("users", userList);
		
		return new ModelAndView("UserList", model);     
	}

	/**
	 * handles default /user/delete request
	 * @param id
	 * @return
	 */
	@RequestMapping(method=RequestMethod.GET,value={"/user/delete"})
	public String deleteUser(
			@RequestParam(value="id", defaultValue="", required=true) int id) {
		
		logger.info("delete " + id);

		userBO.deleteById(id);
		
		return "redirect:/user";
	}

	/**
	 * handles default /user/new request
	 * @return
	 */
	@RequestMapping(method=RequestMethod.GET,value={"/user/new"})
	public ModelAndView newUser() {
		logger.info("new user");
		
		User user = new User();

		return new ModelAndView("NewUser", "user", user);
	}

	/**
	 * handles default /user/save request
	 * @param user
	 * @param bindingResult
	 * @return
	 */
	@RequestMapping(method=RequestMethod.POST,value={"/user/save"})
	public String newUser(@Valid User user, BindingResult bindingResult) {
		logger.info("new user " + user.toString());

		//In case of validation errors, go back to form
		if (bindingResult.hasErrors()) {
			Map<String, Object> model = new HashMap<String, Object>();
			model.put("user", user);
			
			return "NewUser";
		}

		userBO.insert(user);
		
		return "redirect:/user";
	}

	/**
	 * handles default /user/detail request
	 * @param id
	 * @return
	 */
	@RequestMapping(method=RequestMethod.GET,value={"/user/detail"})
	public ModelAndView userDetail(@RequestParam(value="id", defaultValue="", required=true) int id) {
		logger.info("Detail " + id);
		
		Map<String,Object> model = new HashMap<String,Object>();
		User user = userBO.selectById(id);
		model.put("user", user);

		return new ModelAndView("UserDetail", model);
	}


	/**
	 * handles default /user/update request
	 * @param id
	 * @return
	 */
	@RequestMapping(method=RequestMethod.GET,value={"/user/update"})
	public ModelAndView update(@RequestParam(value="id", defaultValue="", required=true) int id) {
		logger.info("update " + id);
		
		Map<String,Object> model = new HashMap<String,Object>();
		User user = userBO.selectById(id);
		model.put("user", user);
		
		return new ModelAndView("UpdateUser",model);
	}


	/**
	 * handles default /user/saveupdate request
	 * @param user
	 * @param bindingResult
	 * @return
	 */
	@RequestMapping(method=RequestMethod.POST,value={"/user/saveupdate"})
	public String saveUpdate(@Valid User user, BindingResult bindingResult) {
		logger.info("update user " + user.toString());

		// In case of validation errors, go back to form
		if (bindingResult.hasErrors()) {
			Map<String, Object> model = new HashMap<String, Object>();
			model.put("user", user);
			
			return "UpdateUser";
		}

		userBO.update(user);
		
		return "redirect:/user";
	}
	
}
